<template>
  <div class="dashboard">
    <div class="header">
      <h1>BI数据看板</h1>
      <div class="filters">
        <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleDateChange"
        />
        <el-button type="primary" @click="refreshData">刷新</el-button>
      </div>
    </div>

    <!-- 数值指标区域 -->
    <div class="metric-grid">
      <bi-chart
          v-for="chartId in numberChartIds"
          :key="chartId"
          :chart-id="chartId"
          :filters="filters"
      />
    </div>

    <!-- 图表区域 -->
    <div class="chart-grid">
      <bi-chart
          v-for="chartId in chartIds"
          :key="chartId"
          :chart-id="chartId"
          :filters="filters"
      />
    </div>

    <!-- 表格区域 -->
    <div class="table-section">
      <h2 class="section-title">销售明细</h2>
      <bi-chart
          chart-id="sales_table"
          :filters="filters"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import BiChart from '@/components/BiChart.vue'

const dateRange = ref([])
const filters = ref({})

const numberChartIds = ref([
  'total_sales',
  'user_count',
  'order_count',
  'conversion_rate'
])

const chartIds = ref([
  'sales_trend',
  'product_distribution',
  'regional_sales'
])

const handleDateChange = (value) => {
  if (value && value.length === 2) {
    filters.value.dateRange = [
      formatDate(value[0]),
      formatDate(value[1])
    ]
  } else {
    delete filters.value.dateRange
  }
}

const formatDate = (date) => {
  return date.toISOString().split('T')[0]
}

const refreshData = () => {
  // 触发子组件重新加载数据
  filters.value = {...filters.value}
}

// 设置默认日期范围为最近30天
onMounted(() => {
  const end = new Date()
  const start = new Date()
  start.setDate(start.getDate() - 400)
  dateRange.value = [start, end]
  handleDateChange(dateRange.value)
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header h1 {
  margin: 0;
  color: #333;
}

.filters {
  display: flex;
  gap: 12px;
  align-items: center;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 24px;
}

.table-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.section-title {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 2px solid #409eff;
  padding-bottom: 8px;
}

@media (max-width: 1200px) {
  .metric-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .chart-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .metric-grid {
    grid-template-columns: 1fr;
  }

  .header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
}
</style>